<template>
  <div class="list gray">
    <!-- <div class="pager-title">{{$route.meta.title}}</div> -->
    <div class="section">
      <div class="panel">
        <div class="amount">
          <p>网商银行可用余额：{{amountData.balance.balance || 0}} 元</p>
          <p>等待审核金额：{{amountData.wait_balance || 0}} 元</p>
        </div>
        <div class="butotn">
          <el-button @click="openDialog()" type="primary" style="width: 100px;">充值</el-button>
        </div>
      </div>
    </div>
    <div class="h2 line">充值记录</div>
    <div class="section">
      <el-table
        :data="tableData"
        v-loading="isLoading"
        style="width: 100%; margin-top: 15px;">
        <!-- <el-table-column
          align="center"
          show-overflow-tooltip
          prop="id"
          sortable="custom"
          label="ID">
        </el-table-column> -->
        <el-table-column
          align="center"
          sortable="custom"
          show-overflow-tooltip
          prop="payer_name"
          label="付款方">
        </el-table-column>
        <el-table-column
          align="center"
          sortable="custom"
          show-overflow-tooltip
          prop="payer_number"
          label="付款账号">
        </el-table-column>
        <el-table-column
          align="center"
          sortable="custom"
          show-overflow-tooltip
          prop="remark"
          label="付款方备注">
        </el-table-column>
        <el-table-column
          align="center"
          sortable="custom"
          show-overflow-tooltip
          prop="amount"
          label="金额">
        </el-table-column>
        <el-table-column
          align="center"
          sortable="custom"
          show-overflow-tooltip
          prop="type"
          label="来源">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 1" size="small" type="danger">充值</el-tag>
            <el-tag v-else size="small">退款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          sortable="custom"
          prop="status"
          label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status === -1" style="color: #F56C6C">审核拒绝</span>
            <span v-else-if="scope.row.status === 1">等待审核</span>
            <span v-else style="color: #67C23A">审核通过</span>

            <el-popover  v-if="scope.row.reason"
              placement="right" width="300" trigger="hover">
              <span slot="reference" style="color: #8896b3;margin-left: 5px;" class="el-icon-info"></span>
              <div class="recharge-info">
                <div class="row">
                  <div class="label">拒绝原因</div>
                  <div class="value">{{scope.row.reason}}</div>
                </div>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <!-- <el-table-column
          align="center"
          sortable="custom"
          prop="recharge_info.reason"
          label="原因">
        </el-table-column> -->
        <el-table-column
          align="center"
          sortable="custom"
          show-overflow-tooltip
          prop="created_at"
          label="时间">
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <el-dialog
      title="充值"
      :visible.sync="formVisible"
      center
      width="600px">
      <el-form ref="form" :model="formData" :rules="rules" label-width="130px">
        <el-form-item label="充值金额" prop="amount">
          <el-input v-model="formData.amount"></el-input>
        </el-form-item>
        <el-form-item label="付款方姓名" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="付款方银行卡号" prop="bank_number">
          <el-input v-model="formData.bank_number"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="desc">
          <el-input type="textarea" v-model="formData.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeForm()">取 消</el-button>
        <el-button type="primary" size="small" @click="submitRecharge()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import {Message} from "element-ui";
export default {
  name: 'list',
  data() {
    return {
      amountData: {
        balance: {
          balance: 0,
        }
      },
      isLoading: false,
      dialogVisible: false,
      formVisible: false,
      formData: {
        type: 1,
        amount: '',
        pay_type: 'mayibank',
        name: '',
        bank_number: '',
        remark: ''
      },
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      tableData: [],
      rules: {
        amount: [
          { required: true, message: '请输入充值金额', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入付款方姓名', trigger: 'blur' }
        ],
        bank_number: [
          { required: true, message: '请输入付款方银行卡号', trigger: 'blur' }
        ],
        recharge_type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
      }
    }
  },
  computed:{
    ...mapGetters([
      'userInfo'
    ])
  },
  methods: {
    closeForm() {
      this.$refs.form.resetFields()
      this.formVisible = false
    },
    submitRecharge() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$http.post('company/recharge/create', {
            ...this.formData
          }).then(res => {
            if (res.code === 0) {
              Message({
                message: '提交成功，等待审核',
                center: true,
                type: 'success'
              })
              this.closeForm()
              this.getDataList()
            }
          })
        }
      })
    },
    getDataList() {
      this.$http.get('company/recharge/list',{
        'page': this.pageIndex,
        'limit': this.pageSize
      }).then(res => {
        if (res.code === 0) {
          this.tableData = res.page.list
          this.totalPage = res.page.totalCount
          this.amountData = res.amount
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    openDialog() {
      this.$http.get('company/recharge/last_info').then(res => {
        if (res.code === 0 && res.recharge) {
          this.formData.bank_number = res.recharge.payer_number
          this.formData.name = res.recharge.payer_number
        }
        this.formVisible = true
      })
    }
  },
  mounted() {
    this.getDataList()
  }
}
</script>
<style lang="less" scoped>
.panel {
  display: flex;
  .amount {
    font-size: 14px;
    width: 300px;
  }
  .butotn {
    flex: 1;
    display: flex;
    align-items: center;
    margin-left: 20px;
  }
}
.tip {
  text-align: center;
  position: relative;
  >img {
    opacity: .3;
    position: absolute;
    width: 160px;
    height: auto;
    left: 50px;
    top: 50%;
    transform: translateY(-35%);
  }
  .title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 20px;
  }
  p {
    margin: 0;
    line-height: 30px;
  }
}

.recharge-info {
  .row {
    display: flex;
    line-height: 24px;
  }
  .label {
    width: 80px;
    text-align: right;
    color: #999;
  }
  .value {
    flex: 1;
    padding-left: 20px;
  }
}
</style>
